package com.hotel.servlet;

import com.hotel.bean.Account;
import com.hotel.dao.AddressDao;
import com.hotel.util.DBOperation;
import org.apache.ibatis.session.SqlSession;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author KungSu.
 * @create 2021-05-13 9:36
 */
@WebServlet(value = "/CommonAddressServlet")
public class CommonAddressServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String type = request.getParameter("type");
        Account account = (Account)request.getSession().getAttribute("curUser");
        SqlSession sqlSession = DBOperation.getSqlSession();
        //新建实体类
        AddressDao addressDao =  sqlSession.getMapper(AddressDao.class);
        List<String> addresses = addressDao.findAddress(account.getAccount_id());
        for (String address:addresses){
            System.out.println(address);
        }

        request.setAttribute("addresses",addresses);
        switch (type){
            case "info":
                sqlSession.close();
                request.getRequestDispatcher("/jsp/wodecahngyongdizhi.jsp").forward(request,response);
                break;
            case "add":
                sqlSession.close();
                request.getRequestDispatcher("/jsp/addAddress.jsp").forward(request,response);
                break;
            case "delete":
                //操作数据库删除
                addressDao.delAddress(request.getParameter("address"));

                List<String> address = addressDao.findAddress(account.getAccount_id());
                sqlSession.close();
                request.setAttribute("addresses",address);
                request.getRequestDispatcher("/jsp/wodecahngyongdizhi.jsp").forward(request,response);
                break;
        }
    }
}
